package com.csdj.controller;

import com.csdj.entity.Tpaper;
import com.csdj.entity.Tuser;
import com.csdj.service.TpaperService;
import com.csdj.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class Usercontroller {
    @Resource
    private UserService userService;

    @Resource
    private TpaperService tpaperService;
@GetMapping("/login")
    public String login(){

    return "login";
    }

    @PostMapping("/main")
    public String main(@RequestParam String account, @RequestParam String password, HttpSession session, Model model, HttpServletResponse response)throws IOException {
        Tuser tuser;
        response.setContentType("text/html;charset=utf-8");
        tuser=userService.login(account,password);

        if(tuser!=null){
            session.setAttribute("username",tuser.getUserName());
            model.addAttribute("username",(String)session.getAttribute("username"));
        return "main";
        }else{
            PrintWriter writer = response.getWriter();
            writer.write("<script>alert('用户名或密码错误')</script>");
            return "login";
        }

    }
    @GetMapping("/lunwen")
    public  String lunWen(){
return "lunWenGuanLi";

    }

    @ResponseBody
    @PostMapping("/lunWenList")
    public List<Tpaper> lunList(){
        List<Tpaper> list=tpaperService.Tpaperlist();

    return list;

    }
    @GetMapping("addLunTan")
    public String luntan(){


    return "addLunWen";

}

@ResponseBody
@GetMapping("/addlunwen")
    public String addlunwen(@RequestParam(value = "title",required = false)String  title,
                            @RequestParam(value = "typeCode",required = false)String  typeCode,
                            @RequestParam(value = "paperSummary",required = false)String  paperSummary
                            ){
            Tpaper tpaper=new Tpaper();
            tpaper.setTitle(title);
            tpaper.setTypeCode(typeCode);
            tpaper.setPaperSummary(paperSummary);
            int insert=tpaperService.Tpaperadd(tpaper);
            if(insert>0){
                return "true";

            }else {
                return "false";
            }

}

@ResponseBody
    @GetMapping("/del")
    public String del(@RequestParam(value = "id",required = false)Integer  id){
  int del=   tpaperService.deleteByPrimaryk(id);
        if(del>1){
            return "true";
        }else{

            return "false";
        }
}


}
